//
//  ShowMessage.m
//  New_Runway
//
//  Created by mack on 2018/3/29.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "ShowMessage.h"

@implementation ShowMessage


+ (void)showHintMessage:(NSString *)message duration:(float)duration withImageName:(NSString *)imageName
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    
    if ([window viewWithTag:8888]) {
        [[window viewWithTag:8888] removeFromSuperview];
    }
    
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor blackColor];
    showview.tag = 8888;
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    
    label.numberOfLines = 0;
    
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenBounds.size.width;
    CGFloat screenHeight = screenBounds.size.height;
    CGFloat currentWidth = screenWidth - 40;
    CGSize boundSize = CGSizeMake(currentWidth, CGFLOAT_MAX);
    
    NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect requiredRect = [message boundingRectWithSize:boundSize options:NSStringDrawingUsesLineFragmentOrigin  attributes:attr context:nil];
    CGSize labelSize = requiredRect.size;
    label.frame = CGRectMake(40, 25/2, labelSize.width, labelSize.height);
    
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:17];
    [showview addSubview:label];
    showview.frame = CGRectMake((screenWidth - labelSize.width - 50)/2, (screenHeight/ 2 + 30) , labelSize.width+40, labelSize.height+25);
    UIImageView *image = [[UIImageView alloc] init];
    image.image = [UIImage imageNamed:imageName];
    [showview addSubview:image];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(showview);
        make.left.equalTo(showview).mas_offset(15);
    }];
    [UIView animateWithDuration:duration animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}

+ (void)showHintMessage:(NSString *)message duration:(float)duration withRemindId:(RemidType)remindId
{
    [self showHintMessage:message duration:duration withRemindId:remindId animationCompleted:nil];
}

+ (void)showHintMessage:(NSString *)message duration:(float)duration withRemindId:(RemidType)remindId animationCompleted:(void (^)(void))completed {
    
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    
    if ([window viewWithTag:8888]) {
        [[window viewWithTag:8888] removeFromSuperview];
    }
    
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor blackColor];
    showview.tag = 8888;
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    
    label.numberOfLines = 0;
    
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenBounds.size.width;
    CGFloat screenHeight = screenBounds.size.height;
    CGFloat currentWidth = screenWidth - 80;
    CGSize boundSize = CGSizeMake(currentWidth, CGFLOAT_MAX);
    
    NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect requiredRect = [message boundingRectWithSize:boundSize options:NSStringDrawingUsesLineFragmentOrigin  attributes:attr context:nil];
    CGSize labelSize = requiredRect.size;
    label.frame = CGRectMake(40, 80, labelSize.width, labelSize.height);
    
    UIImageView *image = [[UIImageView alloc] init];
    if (remindId == remindSuccess) {
        image.image = [UIImage imageNamed:@"successful"];
    }else if (remindId == remindWarning){
        image.image = [UIImage imageNamed:@"warring"];
    }else{
        image.image = [UIImage imageNamed:@"Fail"];
    }
    [showview addSubview:image];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(showview);
        make.centerY.equalTo(showview).mas_offset(-20);
    }];
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:17];
    [showview addSubview:label];
    
    showview.frame = CGRectMake((screenWidth - labelSize.width - 80)/2, (screenHeight -  labelSize.height-100)/2 , labelSize.width+80, labelSize.height+100);
    [UIView animateWithDuration:duration animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
        if (completed) {
            completed();
        }
    }];
}


@end
